-
Notifications
You must be signed in to change notification settings - Fork 10.4k
Mark WebHostBuilder class as obsolete #62785
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Co-authored-by: BrennanConroy <[email protected]>
There are a few places that we should update from WebHostBuilder to HostBuilder Below is an example of what the transformation looks like // Before // After |
Co-authored-by: BrennanConroy <[email protected]>
Updated all four files to use HostBuilder instead of WebHostBuilder following your example pattern. All instances have been converted to use |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add a pragma disable for webhost.cs
Also update src\Identity\samples\IdentitySample.Mvc\Program.cs and src\Servers\Kestrel\Kestrel\test\WebHostBuilderKestrelExtensionsTests.cs to HostBuilder
src/Servers/IIS/IIS/test/testassets/InProcessWebSite/Program.cs
Outdated
Show resolved
Hide resolved
…r pattern Co-authored-by: BrennanConroy <[email protected]>
Added pragma disable for WebHostBuilder.cs and updated both IdentitySample.Mvc/Program.cs and WebHostBuilderKestrelExtensionsTests.cs to use HostBuilder pattern as requested. (5407fb0) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add pragma warning to src/DefaultBuilder/src/WebHost.cs(157,27)
…Builder.cs Co-authored-by: BrennanConroy <[email protected]>
Added pragma warning suppression around the WebHostBuilder instantiation in WebHost.cs CreateDefaultBuilder method. (dd80d9c) |
Could you update src/SignalR/clients/csharp/Client/test/UnitTests/TestServerTests.cs and src/Servers/Kestrel/Kestrel/test/HttpsConfigurationTests.cs to HostBuilder |
@@ -56,22 +56,26 @@ public static int Main(string[] args) | |||
return 12; | |||
case "HangOnStop": | |||
{ | |||
#pragma warning disable CS0618 // Type or member is obsolete |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This app explicitly targets 2.2 packages to make sure our IIS integration is backwards compatible.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Understood. I've kept the WebHostBuilder usage in InProcessWebSite/Program.cs with pragma disables since it explicitly targets 2.2 packages for backwards compatibility testing.
await host.StartAsync(); | ||
await host.StopAsync(); | ||
|
||
var addr = Assert.Single(host.Services.GetRequiredService<IServer>().Features.Get<IServerAddressesFeature>().Addresses); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Addresses aren't set until the host starts, and the address(es) contain the allocated port. Just want to call it our since it's different behavior from WebHostBuilder.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point about the different behavior. With HostBuilder, addresses aren't allocated until the host starts, whereas WebHostBuilder had them available earlier. The converted tests handle this by calling await host.StartAsync() before accessing the test server.
var host = new WebHostBuilder().UseServer(new FakeServer()).Configure(x => { }).Build(); | ||
#pragma warning restore CS0618 // Type or member is obsolete | ||
var webHostService = new WebHostService(host); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
WebHostService
takes an IWebHost
so need to continue using WebHostBuilder
here.
We should probably look into making an IHost
compatible WebHostServer
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Correct. I've kept WebHostServiceTests using WebHostBuilder with pragma disables since WebHostService requires IWebHost. An IHost compatible WebHostServer would indeed be a valuable future enhancement.
@copilot Can you try to convert more of TestServerTests.cs to HostBuilder? |
Co-authored-by: BrennanConroy <[email protected]>
I've converted 12 more TestServerTests.cs methods to use HostBuilder pattern. The file now has 19 HostBuilder instances vs 16 WebHostBuilder instances. The remaining WebHostBuilder tests either test the obsolete TestServer(IWebHostBuilder) constructor specifically or test complex startup scenarios that require the specific behavior of the obsolete API. All tests continue to pass. (161a1b5) |
/azp run |
Azure Pipelines successfully started running 3 pipeline(s). |
Co-authored-by: BrennanConroy <[email protected]>
...DataProtection/DataProtection/test/Microsoft.AspNetCore.DataProtection.Tests/HostingTests.cs
Outdated
Show resolved
Hide resolved
...DataProtection/DataProtection/test/Microsoft.AspNetCore.DataProtection.Tests/HostingTests.cs
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR marks the WebHostBuilder
class as obsolete with the [Obsolete]
attribute to guide developers toward using the modern HostBuilder
approach introduced in ASP.NET Core 3.0. The obsolescence is implemented with a proper diagnostic ID and deprecation message pointing to migration guidance.
Key changes include:
- Added obsolete attribute to
WebHostBuilder
class with diagnostic ID "ASPDEPR004" - Updated numerous test files to use
HostBuilder
withConfigureWebHost()
instead of directWebHostBuilder
usage - Added pragma warnings to suppress obsolete warnings in remaining test code that still needs to test the obsolete functionality
Reviewed Changes
Copilot reviewed 21 out of 21 changed files in this pull request and generated 1 comment.
File | Description |
---|---|
WebHostBuilder.cs | Added obsolete attribute with proper diagnostic ID and deprecation message |
Multiple test files | Modernized to use HostBuilder pattern instead of obsolete WebHostBuilder |
Legacy test files | Added pragma warnings to suppress obsolete warnings where testing legacy functionality |
Sample projects | Updated to use modern HostBuilder approach |
Comments suppressed due to low confidence (2)
src/Hosting/TestHost/test/WebSocketClientTests.cs:152
- There's an extra closing brace that should be removed. The class declaration appears to be properly closed at line 151.
}
src/Hosting/TestHost/test/TestClientTests.cs:1295
- There's an extra closing brace that should be removed. The class appears to be properly closed at line 1294.
}
Co-authored-by: Copilot <[email protected]>
Fixes #20964
This PR marks the
WebHostBuilder
class as obsolete using the[Obsolete]
attribute with a placeholder message as requested in issue #20964.Changes Made
[Obsolete("TODO: Add Obsolete message")]
attribute to theWebHostBuilder
class declarationNoWarn
property:Microsoft.AspNetCore.Hosting.Tests.csproj
Microsoft.AspNetCore.TestHost.Tests.csproj
Microsoft.AspNetCore.Hosting.WindowsServices.Tests.csproj
IStartupInjectionAssemblyName.csproj
Microsoft.AspNetCore.Hosting.TestSites.csproj
Rationale
The
WebHostBuilder
class was replaced byHostBuilder
in ASP.NET Core 3.0, and marking it as obsolete helps guide developers toward the recommendedHostBuilder
approach. The placeholder message follows the pattern requested in the issue and can be updated with a proper deprecation message and migration guidance in a future update.Testing
WebHostBuilder
in projects that don't suppress CS0618This is a minimal, non-breaking change that maintains backward compatibility while alerting developers to the obsolete status of
WebHostBuilder
.This pull request was created as a result of the following prompt from Copilot chat.
💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.